package com.ptu.spzx.common.log.aspect;

import com.ptu.spzx.common.log.annotation.Log;
import com.ptu.spzx.common.log.service.AsyncOperLogService;
import com.ptu.spzx.common.log.utils.LogUtil;
import com.ptu.spzx.model.entity.system.SysOperLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

    @Autowired
    private AsyncOperLogService operLogService;

    @Around(value = "@annotation(sysLog)")
    //环绕通知
    public Object doAroundAdvice(ProceedingJoinPoint joinPoint, Log sysLog) {
//        String title = sysLog.title();
//        int businessType = sysLog.businessType();
//        System.out.println("title:"+title+"businessType:"+businessType);

        //业务方法调用之前，封装数据
        SysOperLog sysOperLog=new SysOperLog();
        LogUtil.beforeHandleLog(sysLog,joinPoint,sysOperLog);
        //业务方法
        Object process = null;
        try {
            process=joinPoint.proceed();
            //调用业务方法之后，封装数据
            LogUtil.afterHandlLog(sysLog,process,sysOperLog,0,null);
//            System.out.println("===========");
        } catch (Throwable e) {
            e.printStackTrace();
            LogUtil.afterHandlLog(sysLog,process,sysOperLog,1,e.getMessage());
            throw new RuntimeException();
        }

        //调用service里面的方法把日志信息添加到数据库里面
        operLogService.saveSysOperLog(sysOperLog);
        return process;
    }
}
